package com.zkthink.ceres.msgs.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.zkthink.base.R;
import com.zkthink.base.service.SuperServiceImpl;
import com.zkthink.ceres.msgs.dao.CeresPushMsgsMapper;
import com.zkthink.ceres.msgs.entity.CeresPushMsgs;
import com.zkthink.ceres.msgs.service.CeresPushMsgsService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

/**
 * <p>
 * 业务实现类
 * 直播的小红心记录
 * </p>
 *
 * @author ck
 * @date 2022-02-28
 */
@Slf4j
@Service
public class CeresPushMsgsServiceImpl extends SuperServiceImpl<CeresPushMsgsMapper, CeresPushMsgs> implements CeresPushMsgsService {
    @Override
    public Boolean ifPushMsgs(Long mid, List<Integer> msgType, long second) {
        List<CeresPushMsgs> ceresPushMsgs = baseMapper.selectIfPushMsgs(mid, msgType, second);
        if (ceresPushMsgs != null && ceresPushMsgs.size() > 0) {
            return true;
        }
        return false;
    }

    @Override
    public Boolean ifLiveAutoPushMsgs(Long mid, String roomId, Integer msgType) {
        List<CeresPushMsgs> ceresPushMsgs = baseMapper.selectLiveIfAutoInvite(mid, roomId, msgType);
        if (ceresPushMsgs != null && ceresPushMsgs.size() > 0) {
            return true;
        }
        return false;
    }

    @Override
    public IPage<CeresPushMsgs> findPushSmsByApp(IPage page, Long mid) {
        return baseMapper.findPushSmsByApp(page, mid);
    }

    @Override
    public R readPushMsg(Long id) {
        CeresPushMsgs msgs = baseMapper.selectById(id);
        if (msgs == null) {
            return R.fail("系统异常,请联系客服");
        }
        msgs.setIsRead(1);
        baseMapper.updateById(msgs);
        return R.success();
    }

    @Override
    public R findPushMsgNum(Long mid) {
        QueryWrapper<CeresPushMsgs> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("rreceive_id", mid).eq("is_read", 0).le("push_time", LocalDateTime.now());
        List<CeresPushMsgs> ceresPushMsgs = baseMapper.selectList(queryWrapper);
        if (ceresPushMsgs == null || ceresPushMsgs.size() == 0) {
            return R.success(0);
        } else {
            return R.success(ceresPushMsgs.size());
        }
    }

}
